package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.InvOrderEntity;
import com.yvtian.inv.dao.InvOrderMapper;
import com.yvtian.inv.pojo.InvOrderShopEntity;
import com.yvtian.inv.service.InvOrderService;
import com.yvtian.inv.service.InvOrderShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * 生产订单(inv_order)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:34
 */
@Service("invOrderService")
public class InvOrderServiceImpl extends ServiceImpl<InvOrderMapper, InvOrderEntity> implements InvOrderService {

    @Autowired
    private InvOrderShopService invOrderShopService;

    @Override
    public IPage<InvOrderEntity> findByPage(Page<InvOrderEntity> page, InvOrderEntity invOrderEntity) {
        LambdaQueryWrapper<InvOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvOrderEntity::getIsDel, 0);
        getQueryWrapper(invOrderEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvOrderEntity invOrderEntity) {
        ArrayList<InvOrderShopEntity> invOrderItemList = invOrderEntity.getInvOrderItemList();
        if (invOrderItemList != null && !invOrderItemList.isEmpty()) {
            LambdaUpdateWrapper<InvOrderShopEntity> itemUpdateWrapper = new LambdaUpdateWrapper<>();
            itemUpdateWrapper.eq(InvOrderShopEntity::getZcode, invOrderEntity.getCode());
            invOrderShopService.remove(itemUpdateWrapper);
            invOrderShopService.saveBatch(invOrderItemList);
        }
        this.saveOrUpdate(invOrderEntity);
        return true;
    }

    private void getQueryWrapper(InvOrderEntity invOrderEntity, LambdaQueryWrapper<InvOrderEntity> queryWrapper) {
        if (StringHelper.isNotEmpty(invOrderEntity.getStartTimeStr())) {
            String[] split = invOrderEntity.getStartTimeStr().split("~");
            queryWrapper.between(InvOrderEntity::getStartTime, split[0], split[1]);
        }
        if (StringHelper.isNotEmpty(invOrderEntity.getEndTimeStr())) {
            String[] split = invOrderEntity.getEndTimeStr().split("~");
            queryWrapper.between(InvOrderEntity::getEndTimeStr, split[0], split[1]);
        }
        queryWrapper
                .like(StringHelper.isNotEmpty(invOrderEntity.getCode()), InvOrderEntity::getCode, invOrderEntity.getCode())
                .like(StringHelper.isNotEmpty(invOrderEntity.getPlanCode()), InvOrderEntity::getPlanCode, invOrderEntity.getPlanCode())
                .like(StringHelper.isNotEmpty(invOrderEntity.getBomCode()), InvOrderEntity::getBomCode, invOrderEntity.getBomCode())
                .like(StringHelper.isNotEmpty(invOrderEntity.getBomName()), InvOrderEntity::getBomName, invOrderEntity.getBomName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getShopCode()), InvOrderEntity::getShopCode, invOrderEntity.getShopCode())
                .like(StringHelper.isNotEmpty(invOrderEntity.getShopName()), InvOrderEntity::getShopName, invOrderEntity.getShopName())
                .eq(invOrderEntity.getMakeQuantity() != null, InvOrderEntity::getMakeQuantity, invOrderEntity.getMakeQuantity())
                .like(StringHelper.isNotEmpty(invOrderEntity.getLeaderUsername()), InvOrderEntity::getLeaderUsername, invOrderEntity.getLeaderUsername())
                .like(StringHelper.isNotEmpty(invOrderEntity.getLeaderAccount()), InvOrderEntity::getLeaderAccount, invOrderEntity.getLeaderAccount())
                .like(StringHelper.isNotEmpty(invOrderEntity.getProductionStatus()), InvOrderEntity::getProductionStatus, invOrderEntity.getProductionStatus())
                .like(StringHelper.isNotEmpty(invOrderEntity.getProjectCode()), InvOrderEntity::getProjectCode, invOrderEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(invOrderEntity.getProjectName()), InvOrderEntity::getProjectName, invOrderEntity.getProjectName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getMaterialRequestStatus()), InvOrderEntity::getMaterialRequestStatus, invOrderEntity.getMaterialRequestStatus())
                .eq(invOrderEntity.getStatus() != null, InvOrderEntity::getStatus, invOrderEntity.getStatus())
                .like(StringHelper.isNotEmpty(invOrderEntity.getDelName()), InvOrderEntity::getDelName, invOrderEntity.getDelName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getCreateName()), InvOrderEntity::getCreateName, invOrderEntity.getCreateName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getCreateUser()), InvOrderEntity::getCreateUser, invOrderEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(invOrderEntity.getUpdateName()), InvOrderEntity::getUpdateName, invOrderEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getUpdateUser()), InvOrderEntity::getUpdateUser, invOrderEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(invOrderEntity.getRemarks()), InvOrderEntity::getRemarks, invOrderEntity.getRemarks())
                .like(StringHelper.isNotEmpty(invOrderEntity.getCheckName()), InvOrderEntity::getCheckName, invOrderEntity.getCheckName())
                .like(StringHelper.isNotEmpty(invOrderEntity.getCheckUser()), InvOrderEntity::getCheckUser, invOrderEntity.getCheckUser());
    }
}

